//
//  AFRequdstAndLoad.m
//  LiangYa_BObject
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 YuHao. All rights reserved.
//

#import "AFRequdstAndLoad.h"

@implementation AFRequdstAndLoad

+ (void)dataURLStr:(NSString *)URLStr datahand:(void (^)(id))block
{  
    UIApplication *application = [UIApplication sharedApplication];
    application.networkActivityIndicatorVisible = YES;
    
    URLStr = [URLStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    NSString *strPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [NSString stringWithFormat:@"%@/%lu.txt",strPath,(unsigned long)[URLStr hash]];
    
    [manager GET:URLStr parameters:nil success:^(NSURLSessionDataTask * task, id responseObject) {
        if (responseObject != nil) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        
        block(responseObject);
        [data writeToFile:path atomically:YES];
        application.networkActivityIndicatorVisible = NO;
        }
    }
     
         failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
             
             NSData *data = [NSData dataWithContentsOfFile:path];
             if (data != nil) {
                 id data1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                 block(data1);
             }
             application.networkActivityIndicatorVisible = NO;
         }];
    
}

+ (NSString *)requestCachePath:(NSString *)requestUrl
{
    // 取出缓存路径
    NSString *cachesPath = [self cachePath];
    
    // 文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    // 如果路径不存在,则创建缓存文件夹
    if (![fileManager isExecutableFileAtPath:cachesPath]) {
        [fileManager createDirectoryAtPath:cachesPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    // 生成该请求对ing的缓存文件的文件名
    NSString *requestCacheFileName = [NSString stringWithFormat:@"/%@.aa",[self md5:requestUrl]];
    NSLog(@"缓存的地址:%@",cachesPath);
    NSLog(@"缓存的文件名:%@",requestCacheFileName);
    // 拼接该请求的缓存路径
    NSString *rquestCacheFilePath = [cachesPath stringByAppendingString:requestCacheFileName];
    
    return rquestCacheFilePath;
}

+ (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
//    CC_MD5(cStr, (int)strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

/*
 * 缓存路径
 */
+ (NSString *)cachePath
{
    // 取出缓存路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    // 拼接存放缓存的文件夹路径
    NSString *newCachesPath = [cachesPath stringByAppendingPathComponent:@"myCaches"];
    
    return newCachesPath;
}

/*
 * 清空缓存
 */
+ (void) clearCaches
{
    // 存放缓存的文件夹路径
    NSString *cachePath = [self cachePath];
    
    // 创建文件管理对象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    // 如果该缓存路径存在,则删除该缓存文件夹
    if ([fileManager fileExistsAtPath:cachePath]) {
        [fileManager removeItemAtPath:cachePath error:nil];
    }
    
    // 清理图片缓存
    SDImageCache *sdImage = [SDImageCache sharedImageCache];
    [sdImage clearDisk];
    //    [sdImage clearMemory];
}

/*
 * 缓存大小
 */
+ (CGFloat) getSizeOfCaches
{
    // 存放缓存的文件夹路径
    NSString *newCachesPath = [self cachePath];
    
    // 创建文件管理对象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    CGFloat cacheSize = [[fileManager attributesOfItemAtPath:newCachesPath error:nil] fileSize];
    
    // 图片缓存
    SDImageCache *sdImage = [SDImageCache sharedImageCache];
    NSUInteger sdSize = [sdImage getSize];
    
    return cacheSize+sdSize;
}




@end
